package give.mapreplay;

import de.saar.penguin.give2.mapviewer.JMapViewer;
import de.saar.penguin.give2.mapviewer.notification.Drawable;
import de.saar.penguin.give2.mapviewer.notification.NotificationPlugin;
import give.util.StringTools;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: koller
 * Date: Oct 19, 2009
 * Time: 11:28:15 AM
 * To change this template use File | Settings | File Templates.
 */
public class TextNotificationPlugin extends NotificationPlugin {
    private static final Font FONT = new Font("Helvetica", Font.PLAIN, 18);
    private static final int STARTX = 50;
    private static final int STARTY = 50;
    private static final int BORDER = 3;

    public TextNotificationPlugin(JMapViewer viewer) {
        super(viewer);
    }

    public void addTextAnnotation(String string) {
        final List<String> lines = StringTools.wordwrap(string, 80);

        clear();
        addAnnotation(new Drawable() {
            public void draw(Graphics2D graphics) {
                graphics.setFont(FONT);
                FontMetrics fm = graphics.getFontMetrics();
                Rectangle2D stringRect = fm.getStringBounds(lines.get(0), graphics);

                // determine maximum width of strings
                double width = stringRect.getWidth();
                for (String s : lines) {
                    width = Math.max(width, fm.getStringBounds(s, graphics).getWidth());
                }

                // adjust starting x position to the left to make sure that strings fit in window
                double startx = stringRect.getX() + STARTX;
                if (startx + width > viewer.getWidth()) {
                    startx = viewer.getWidth() - width;
                }


                Rectangle2D rect = new Rectangle2D.Double(startx-BORDER, stringRect.getY() + STARTY - BORDER, width + 2*BORDER, lines.size() * stringRect.getHeight() + 2*BORDER);

                graphics.setColor(new Color(1, 1, 1, 0.8f));
                graphics.fill(rect);

                graphics.setColor(Color.black);
                graphics.draw(rect);

                int yoff = 0;
                for (String s : lines) {
                    graphics.drawString(s, (int) startx, STARTY + yoff);
                    yoff += stringRect.getHeight();
                }
            }
        }, 5000);
    }
}
